﻿using System.Collections.Generic;
using System.Linq;
using Common.Dao;
using Common.Extension;
using Common.Model;
using Spider.Mode;
using Spider.Spider;
using Spider.Util;

namespace Spider.Script
{
    public class DxwScript
    {
        public static void Run()
        {
            List<ConceptClassify> conceptClassifies =
                DxwSpider.GetClassify().Where(m => !"按大类".Equals(m.ClassifyName)).ToList();

            Dictionary<string, List<DxwEvent>> dictionary = DxwSpider.GetAllEvents();

            List<DxwBlock> concepts = new List<DxwBlock>();
            foreach (ConceptClassify conceptClassify in conceptClassifies)
            {
                concepts.AddRange(conceptClassify.Concept.Select(m => new DxwBlock()
                {
//                    BlockCode = m.ConceptCode,
                    BlockName = m.ConceptName,
//                    Id = Util.Util.GetMd5(m.ConceptName+conceptClassify.ClassifyName).ToUpper()
                }));
            }

            DxwBlockDao.BatchSaveDxwBlock(concepts);

            List<DxwEvent> dxwEvents = new List<DxwEvent>();
            foreach (var events in dictionary.Values.ToList())
            {
                dxwEvents.AddRange(events);
            }

            dxwEvents = dxwEvents.Distinct(new EventCompartor()).ToList();
  
            dxwEvents = dxwEvents.Where(m =>!string.IsNullOrEmpty(m.Block)).ToList();
            List<string> classifyBlock = ClassifyModelDao.GetClassifyBlock().ToList();
            Dictionary<string, string> dxwBlocks = DxwBlockDao.GetDxwBlock().Distinct(new BlockCompartor())
                .ToDictionary(n => n.BlockName, m => m.TopicName);
            foreach (var block in classifyBlock)
            {
                if (!dxwBlocks.ContainsKey(block))
                {
                    dxwBlocks.Add(block, "");
                }
            }
            var allTopics = MainTopicDao.GetAllTopics();

            var keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.AddRange(dxwBlocks, true);
            foreach (var keyValuePair in keyValuePairs)
            {
                foreach (var allTopic in allTopics)
                {
                    if ((allTopic.MergeName + allTopic.TopicName).Contains(keyValuePair.Key) &&
                        string.IsNullOrEmpty(keyValuePair.Value))
                    {
                        if (dxwBlocks.ContainsKey(keyValuePair.Key))
                        {
                            dxwBlocks.Remove(keyValuePair.Key);
                        }
                        dxwBlocks.Add(keyValuePair.Key, allTopic.TopicName);
                    }
                }
            }

            Util.Util.DxwBlockMapping(dxwEvents, dxwBlocks);
            DxwEventDao.BatchSaveEvents(dxwEvents);

//            var dxwBlocksDic = DxwBlockDao.GetDxwBlock().Distinct(new BlockCompartor())
//                .ToDictionary(block => block.BlockName, block => block.TopicName ?? "");

//            List<News> news = Util.Util.Dxwevents2News(dxwEvents);
//            Util.Util.DxwBlockMapping(news, dxwBlocksDic);
//            NewsDao.BatchSaveNews(news);
        }
    }
}